package com.sugon.xuanyuan;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class LPRImageStorage {
    private static final String DB_URL = "jdbc:mysql://100.150.0.123:3306/test";
    private static final String USER = "root";
    private static final String PASS = "admin1234@sugon";

    public static void main(String[] args) {
        String imagePath = "C:\\Users\\admin\\Desktop\\20250731-180504.jpg";
        String imageName = "test2";

        try {
            // 存储图片到数据库
            storeImage(imagePath, imageName);

            // 从数据库读取图片
//            byte[] imageData = retrieveImage(imageName);
//            if (imageData != null) {
//                saveImageToFile(imageData, "retrieved_image.jpg");
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void storeImage(String imagePath, String imageName) throws Exception {
        Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
        PreparedStatement pstmt = conn.prepareStatement("INSERT INTO lpr_image (name, image_data) VALUES (?, ?)");

        FileInputStream fis = new FileInputStream(imagePath);
        pstmt.setString(1, imageName);
        pstmt.setBinaryStream(2, fis, (int) new File(imagePath).length());
        pstmt.executeUpdate();


        pstmt.close();
        conn.close();
    }


}
